#include <stdlib.h>

#include "CuTest.h"

#include "test_sprout.h"
#include "sprout.h"
#include "sprout_util.h"

void test_c_array_to_json_array_empty(CuTest *tc)
{
    const char *ex = "[]";
    Array arr = {};
    char *ac = c_array_to_json_array(&arr);
    CuAssertStrEquals(tc, ex, ac);
    free_memory(ac);
}

void test_json_array_to_c_array_empty(CuTest *tc)
{
    char *use = read_file("./tests/data/array_empty.json");
    const char *use_ = use;
    Array *ac = json_array_to_c_array(&use_);
    CuAssertPtrNotNull(tc, ac);
    CuAssertPtrEquals(tc, NULL, ac->value);
    free_array(ac);
    free_memory(use);
}

void test_json_array_to_c_array_empty_composite(CuTest *tc)
{
    char *use = read_file("./tests/data/array_empty_composite.json");
    const char *use_ = use;
    Array *ac = json_array_to_c_array(&use_);
    CuAssertPtrNotNull(tc, ac);
    CuAssertPtrNotNull(tc, ac->value);
    CuAssertPtrNotNull(tc, ac->value->next);
    CuAssertPtrEquals(tc, NULL, ac->value->next->next);
    free_array(ac);
    free_memory(use);
}

void test_c_array_to_json_array_empty_composite(CuTest *tc)
{
    const char *ex = "[{},[],{}]";
    Object obj = {};
    Array arr_ = {};
    Value empty01 = {.t = J_OBJECT, .v = &obj};
    Value empty02 = {.t = J_ARRAY, .v = &arr_};
    Value empty03 = {.t = J_OBJECT, .v = &obj};
    empty01.next = &empty02;
    empty02.next = &empty03;
    Array arr = {.value = &empty01};
    char *ac = c_array_to_json_array(&arr);
    CuAssertStrEquals(tc, ex, ac);
    free_memory(ac);
}

CuSuite *test_array_suite()
{
    CuSuite *suite = CuSuiteNew();
    SUITE_ADD_TEST(suite, test_c_array_to_json_array_empty);
    SUITE_ADD_TEST(suite, test_json_array_to_c_array_empty);
    SUITE_ADD_TEST(suite, test_json_array_to_c_array_empty_composite);
    SUITE_ADD_TEST(suite, test_c_array_to_json_array_empty_composite);
    return suite;
}
